#!/bin/bash


install_docker(){
  echo "环境安装："
  yum -y install gcc-c++

  echo "第一步：安装必要的一些系统工具"
  yum install -y yum-utils device-mapper-persistent-data lvm2

  echo "第二步：添加软件源信息"
  yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

  echo "第三步：更新并安装Docker-CE"
  yum makecache fast
  yum -y install docker-ce

  echo "第四步：开启Docker服务"
  service docker start
  systemctl enable docker

  echo "第五步：测试是否安装成功"
  docker -v

  echo "Install Docker Compose"
  sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  sudo chmod +x /usr/local/bin/docker-compose
}

save_images(){
  echo "删除tar："
  rm ./my_images.tar
  echo "下载镜像："
  while IFS= read -r image; do
    [[ -n "$image" ]] && docker pull "$image"
  done < images.list
  echo "保存tar："
  docker save -o ./my_images.tar $(cat images.list)

  # sudo docker load --input ./my_images.tar
}

install_container(){
  echo "Create a directory for Docker Compose files"
  mkdir -p ~/docker-compose
  cd ~/docker-compose

  # Download the RabbitMQ Delayed Message Exchange plugin
  wget https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/v3.12.0/rabbitmq_delayed_message_exchange-3.12.0.ez -P ~/docker-compose

# Create a docker-compose.yml file
cat <<EOF > docker-compose.yml
version: '3.8'

services:
  mysql:
    image: mysql:8.0.29
    container_name: share_mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypassword
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    command: --default-authentication-plugin=mysql_native_password --bind-address=0.0.0.0

  redis:
    image: redis:7.0.10
    container_name: share_redis
    restart: always
    ports:
      - "6379:6379"

  nacos:
    image: nacos/nacos-server:v2.1.1
    container_name: share_nacos2.1.1
    restart: always
    environment:
      MODE: standalone
    ports:
      - "8848:8848"
      - "9848:9848"
      - "9849:9849"

  minio:
    image: minio/minio
    container_name: share_minio
    restart: always
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: admin
      MINIO_ROOT_PASSWORD: admin123456
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_data:/data
      - minio_config:/root/.minio

  rabbitmq:
    image: rabbitmq:3.12.0-management
    container_name: share_rabbitmq
    restart: always
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: guest
      RABBITMQ_DEFAULT_PASS: guest
    volumes:
      - ./rabbitmq_delayed_message_exchange-3.12.0.ez:/plugins/rabbitmq_delayed_message_exchange-3.12.0.ez
    command: >
      bash -c "
      rabbitmq-plugins enable rabbitmq_delayed_message_exchange &&
      rabbitmq-server
      "

  mongodb:
    image: mongo:7.0.0
    container_name: share_mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root_password
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db

  emqx:
    image: emqx/emqx:5.7.0
    container_name: share_emqx
    restart: always
    ports:
      - "1883:1883"
      - "8083:8083"
      - "8084:8084"
      - "8883:8883"
      - "18083:18083"
    volumes:
      - emqx_data:/opt/emqx/data
      - emqx_log:/opt/emqx/log

volumes:
  mysql_data:
  minio_data:
  minio_config:
  mongo_data:
  emqx_data:
  emqx_log:
EOF

  # Stop and remove any running containers
  docker-compose down

  # Remove any unused networks and volumes
  docker network prune -f
  docker volume prune -f

  # Pull the latest images
  docker-compose pull

  echo "MySQL setup complete with remote root access."

  # Start the services
  docker-compose up -d

  # Output access information
  echo "Access Information:"
  echo "-------------------"
  echo "MySQL:"
  echo "  URL: jdbc:mysql://localhost:3306/mydatabase"
  echo "  Username: myuser"
  echo "  Password: mypassword"
  echo
  echo "Redis:"
  echo "  URL: redis://localhost:6379"
  echo
  echo "Nacos:"
  echo "  URL: http://localhost:8848/nacos"
  echo "  Username: nacos"
  echo "  Password: nacos"
  echo
  echo "MinIO:"
  echo "  URL: http://localhost:9001"
  echo "  Username: admin"
  echo "  Password: admin123456"
  echo
  echo "RabbitMQ:"
  echo "  URL: http://localhost:15672"
  echo "  Username: guest"
  echo "  Password: guest"
  echo
  echo "MongoDB:"
  echo "  URL: mongodb://root:root_password@localhost:27017/"
  echo
}

# 使用说明，用来提示输入参数
usage() {
	echo "Usage: sh 执行脚本.sh [1 install_docker|2 install_container|3 save_images|all]"
	exit 1
}

# 根据输入参数，选择执行对应方法，不输入则执行使用说明
case "$1" in
  "1")
    install_docker
  ;;
  "2")
    install_container
  ;;
  "3")
    save_images
  ;;
  "all")
    install_docker
    install_container
  ;;
  *)
    usage
  ;;
esac
